//
//  PasswordListViewController.swift
//  FearfulNotification
//
//  Created by Jabin on 2019/9/24.
//  Copyright © 2019 筱鹏. All rights reserved.
//

import UIKit

class PasswordListViewController: BaseViewController {

    // 还原状态
    enum RestoreStateKey {
        case active
        case firstResponder
        case searchText
    }
    
    struct RestoreState {
        var isActive = false
        var isFirstResponder = false
        var searchText: String? = nil
    }
    
    private var listModel = PasswordListModel()
    private var tableView: UITableView!
    private var searchVC: UISearchController!
    private var reState = RestoreState()
    
    //MARK: Life Cycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configSubviews()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if reState.isActive {
            searchVC.isActive = reState.isActive
            reState.isActive = false
            if reState.isFirstResponder {
                searchVC.searchBar.becomeFirstResponder()
                reState.isFirstResponder = false
            }
        }
    }
    
    //MARK: Configure
    
    private func configSubviews() {
        tableView = UITableView(frame: view.bounds, style: .plain)
        tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)
        
        let resultVC = PasswordListResultViewController()
        resultVC.tableView.delegate = self
        searchVC = UISearchController(searchResultsController: resultVC)
        searchVC.dimsBackgroundDuringPresentation = false
        searchVC.hidesNavigationBarDuringPresentation = false
        searchVC.delegate = self
        searchVC.searchBar.delegate = self
    }

}

extension PasswordListViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listModel.datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
}

extension PasswordListViewController: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
}

extension PasswordListViewController: UISearchBarDelegate {

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        
    }
    
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        
        
    }
}

extension PasswordListViewController: UISearchControllerDelegate {
    
}
